<?php

namespace app\admin\controller;

use app\BaseController;
use app\common\model\PatientModel;

class  Patient extends BaseController
{
    /**
     * @var PatientModel
     */
    private $model;

    public function initialize()
    {
        $this->model = new PatientModel();
    }

    public function index()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $option = $this->option();
        $result["status"] = "ok";
        $result["msg"] = "查找成功";
        $result["data"] = $this->model->pageData($option);

        return json($result);
    }

    public function create()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $insertData = [
            'name' => $this->request->param("name", ""),
            'sex' => $this->request->param("sex", ""),
            'age' => $this->request->param("age", ""),
            'mobile' => $this->request->param("mobile", ""),
            'idcard' => $this->request->param("idcard", ""),
            'address' => $this->request->param("address", ""),
        ];

        if (empty($insertData["name"])) {
            $result["msg"] = "请输入标签名称";
            return json($result);
        }

        $result = $this->model->saveData($insertData);
        return json($result);
    }

    public function update()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $id = $this->request->param("id", 0);
        $infoData = $this->request->param("info", "");
        if (empty($id)) {
            $result["msg"] = "id参数缺少";
            return json($result);
        }
        if (empty($infoData)) {
            $result["msg"] = "info参数缺少";
            return json($result);
        }
        $info = is_array($infoData) ? $infoData : json_decode($infoData, 1);
        $updateData = [
            'name' => $info['name'],
            'sex' => $info['sex'],
            'age' => $info['age'],
            'mobile' => $info['mobile'],
            'idcard' => $info['idcard'],
            'address' => $info['address']
        ];
        $result = $this->model->editData($id, $updateData);
        return json($result);
    }

    public function delete()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        return json($result);
    }
}